/* const configDef = {}
const config = 'config'
configDef.test = 'test'
configDef.get = ()=> config
if(process.env.NODE_ENV !== 'production'){
    configDef.set = ()=>{
        console.log(1)
    }
}
console.log(configDef) */

const options = {
    el:'#el',
    data:{
        test:1
    },
    methods: {
        fn(){
            console.log('testfn')
        }
    },
}
function Vue(options){  
    this._init(options)
}
initMixin(Vue)
stateMixin(Vue)
function initMixin(Vue){
    Vue.prototype._init = function (){
        console.log('_init')
    }
}
const config2 = {
    name:'YC',
    age:'30',
    work:'打工人'
}
function stateMixin(Vue){
    const obj1 = {}
    obj1.get= ()=> 'config1'
    obj1.set = ()=>{ console.log('set obj1') }

    const obj2 = {}
    obj2.get = ()=> config2

    Object.defineProperty(Vue.prototype,'$data',obj1)
    Object.defineProperty(Vue.prototype,'$props',obj2)
}
let vm = new Vue(options)
console.log(Vue.prototype["$props"])

// --------------

Vue.options = Object.create(null)
const ASSET_TYPES = ['component','directive','filter']
ASSET_TYPES.forEach(type=>{
    Vue.options[type + 's'] = Object.create(null)
})
console.log('-------Vue.options------')
console.log(Vue.options)
// ------Vue.use-------
console.log('------------Vue.use-----------')
// 从start下标截取数组元素，存入新数组
function toArray (list,start){
    start = start || 0
    var i = list.length - start
    var ret = new Array(i)
    while(i--){
        ret[i] = list[start + i]
    }
    return ret
}
function initUse(Vue){
    Vue.use = function(plugin){
        // 将所有安装的插件添加到installedPlugins数组中
        const installedPlugins = (this._installedPlugins || (this._installedPlugins = []))
        if(installedPlugins.indexOf(plugin)>-1){
            return this
        }
        
        // 把数组中的第一个元素plugin去除
        const args = toArray(arguments,1)
        // 把this(即Vue)插入第一个元素的位置,需要传入当前Vue作为参数
        args.unshift(this)
        if(typeof plugin.install==='function'){
            // 调用插件的install方法
            plugin.install.apply(plugin,args)
        }else{
            // 如果插件plugin是对象，则将对象传入install方法
            install.apply(null,args)
        }
        // 将安装的插件push进数组中
        installedPlugins.push(plugin)
        console.log(installedPlugins)
        return this
    }
}
// 初始化Vue.use()方法
initUse(Vue)
let plugin = {
    name:'插件',
    install(){
        console.log('这是install方法')
    }
}
// 调用Vue.use()方法
Vue.use(plugin,'param1','param2','param3')

// -----Vue.mixin()------
function mergeOptions(){
    // ...
}
function initMixin(Vue){
    Vue.mixin = function (mixin){
        // mergeOptions负责把mixin中所有成员拷贝包Vue的options选项中
        this.options = mergeOptions(this.options,mixin)
        return this
    }
}
initMixin(Vue)
Vue.mixin({
    name:'YC',
    age:30
})
// -----initExtend---